import prompt from '@system.prompt';
import router from '@system.router';
import UserApi from '../../api/userApi.js'
import dataStorage from '@ohos.data.storage'

export default {
    data: {
        title: '',
        column: '',
        data: null,
        user: {},
        new_password:null,
    },
    onInit() {
        this.title = router.getParams().title;
        this.column = router.getParams().column;
        this.data = router.getParams().data;
        this.user = router.getParams().currentUser;
    },
    goBack() {
        router.back();
    },
    setNickname(e) {
        this.user.nickname = e.value
    },
    setPassword(e) {
        this.user.password = e.value
    },
    setPassword2(e) {
        this.new_password = e.value
    },
    setPhone(e) {
        this.user.phone = e.value
    },
    setEmail(e) {
        this.user.email = e.value
    },
    setQQNumber(e) {
        this.user.qqNumber = e.value
    },
    setSignature(e) {
        this.user.signature = e.text
    },
    submit() {
        let that = this;
        if(this.column == 'password'){
            if(this.new_password!=this.user.password){
                prompt.showToast({
                    message: '两次输入的密码不一致，请重新输入！',
                })
            }else{
                UserApi.updatePassword(this.user, (data) => {
                    prompt.showToast({
                        message: '修改成功'
                    })
                    let storage = dataStorage.getStorageSync(that.$app.$def.globalData.path);
                    storage.putSync('user', JSON.stringify(that.user));
                    storage.flushSync();
                    that.$app.$def.globalData.user = that.user;
                });
            }
        }
        else{
            UserApi.modify(this.user, (data) => {
                prompt.showToast({
                    message: '修改成功'
                })
                let storage = dataStorage.getStorageSync(that.$app.$def.globalData.path);
                storage.putSync('user', JSON.stringify(that.user));
                storage.flushSync();
                that.$app.$def.globalData.user = that.user;
            });
        }

    }
}
